<template>
  <div style="padding: 10px">
    <div style="padding: 10px 0">
      <!--      功能区域-->
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
    </div>
    <!--搜索区域-->
    <div style="margin: 10px 0">
      <el-input
        v-model="search"
        placeholder="请输入标题关键字"
        style="width: 20%"
        :clearable="true"
      ></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" sortable> </el-table-column>

      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="satisfy" label="满多少元可以使用"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="createTime" label="活动开始时间"> </el-table-column>
      <el-table-column prop="updateTime" label="活动结束时间"> </el-table-column>
      <el-table-column prop="conditions" label="限制条件"> </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除吗" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>

      <!--    新增用户弹窗-->
      <el-dialog v-model="dialogVisible" title="提示" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="价格">
            <el-input v-model="form.price" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="满多少元可以使用">
            <el-input v-model="form.satisfy" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="标题">
            <el-input v-model="form.title" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="活动开始时间">
            <el-date-picker
              v-model="form.createTime"
              value-format="YYYY-MM-DD"
              type="date"
              style="width: 80%"
              clearable="true"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="活动结束时间">
            <el-date-picker
              v-model="form.updateTime"
              value-format="YYYY-MM-DD"
              type="date"
              style="width: 80%"
              clearable="true"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="限制条件">
            <el-input v-model="form.conditions" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { unref, ref } from 'vue'

const form = ref<Record<string, any>>({
  images: undefined
})
const dialogVisible = ref(false)
const search = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const tableData = ref([])

const load = async () => {
  request
    .get('/coupons', {
      params: {
        pageNum: currentPage.value,
        pageSize: pageSize.value,
        search: search.value
      }
    })
    .then((res) => {
      console.log(res)
      tableData.value = res.data.records
      total.value = res.data.total
    })
}

load()

const add = () => {
  dialogVisible.value = true
  form.value = {} //清空前面数据
}

const save = () => {
  if (form.value.id) {
    //更新
    request.put('/coupons', unref(form)).then((res: any) => {
      console.log(res)
      if (res.code === '200') {
        ElMessage.success('更新成功')
      } else {
        ElMessage.error(res.msg)
      }
      load() // 刷新表格的数据
      dialogVisible.value = false
    })
  } else {
    // 新增
    request.post('/coupons', unref(form)).then((res: any) => {
      console.log(res)
      if (res.code === '200') {
        ElMessage.success('新增成功')
      } else {
        ElMessage.error(res.msg)
      }
      load() // 刷新表格的数据
      dialogVisible.value = false
    })
  }
}

const handleEdit = (row: any) => {
  form.value = JSON.parse(JSON.stringify(row))
  dialogVisible.value = true
}
const handleSizeChange = (size: number) => {
  pageSize.value = size
  load()
}
const handleCurrentChange = (num: number) => {
  currentPage.value = num
  load()
}
const handleDelete = (id: number) => {
  console.log(id)
  request.delete('/commoditys/' + id).then((res: any) => {
    //   /" + id通过这个映射到后台“/{id}”
    console.log(res)
    if (res.code === '200') {
      // this.$message({
      //   type: "success",
      //   message: "删除成功"
      // })
    } else {
      // this.$message({
      //   type: "error",
      //   message: res.msg
      // })
    }
    load() // 刷新表格的数据
  })
}
</script>
